import dashscope
from model.model import Model

class DashscopeModel(Model):
    '''使用国产大模型，通过dashcope访问百炼平台相关模型，执行相关对话'''

    def __init__(self, model_name:str, api_key:str) -> None:
        self.model_name = model_name
        self.api_key = api_key

    def make_request(self, prompt):
        messages = [
            {'role': 'user', 'content': prompt}
        ]
        print(messages)
        try:
            response = dashscope.Generation.call(
                model=self.model_name,
                api_key=self.api_key,
                messages=messages,
                result_format='message')
            print(response)
            translation = response.output.choices[0].message.content.strip()
            return translation, True
        except Exception as e:
            print(e)
            return "", False